package com.ruoyi.yunxin.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.util.voice.VoiceUtil;
import com.ruoyi.util.voice.vo.request.MiddleNumberUnbindVo;
import com.ruoyi.yunxin.domain.MiddleRelation;
import com.ruoyi.yunxin.service.IMiddleRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小号绑定记录Controller
 *
 * @author ruoyi
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/middleRelation/middleRelation")
public class MiddleRelationController extends BaseController {
    @Autowired
    private IMiddleRelationService middleRelationService;

    @Resource
    private VoiceUtil voiceUtil;

    /**
     * 查询小号绑定记录列表
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:list')")
    @GetMapping("/list")
    public TableDataInfo list(MiddleRelation middleRelation) {
        startPage();
        List<MiddleRelation> list = middleRelationService.selectMiddleRelationList(middleRelation);
        return getDataTable(list);
    }

    /**
     * 导出小号绑定记录列表
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:export')")
    @Log(title = "小号绑定记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiddleRelation middleRelation) {
        List<MiddleRelation> list = middleRelationService.selectMiddleRelationList(middleRelation);
        ExcelUtil<MiddleRelation> util = new ExcelUtil<MiddleRelation>(MiddleRelation.class);
        util.exportExcel(response, list, "小号绑定记录数据");
    }

    /**
     * 获取小号绑定记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(middleRelationService.selectMiddleRelationById(id));
    }

    /**
     * 新增小号绑定记录
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:add')")
    @Log(title = "小号绑定记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiddleRelation middleRelation) {
        return toAjax(middleRelationService.insertMiddleRelation(middleRelation));
    }

    /**
     * 修改小号绑定记录
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:edit')")
    @Log(title = "小号绑定记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiddleRelation middleRelation) {
        return toAjax(middleRelationService.updateMiddleRelation(middleRelation));
    }

    /**
     * 删除小号绑定记录
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:remove')")
    @Log(title = "小号绑定记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(middleRelationService.deleteMiddleRelationByIds(ids));
    }


    /**
     * 解绑小号
     */
    @PreAuthorize("@ss.hasPermi('middleRelation:middleRelation:remove')")
    @Log(title = "解绑小号", businessType = BusinessType.DELETE)
    @GetMapping("unbindNumber")
    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult unbindNumber(@RequestParam Long id) throws Exception {
        MiddleRelation middleRelation = middleRelationService.selectMiddleRelationById(id);
        if (CheckEmptyUtil.isEmpty(middleRelation)) {
            throw new RuntimeException("记录不存在");
        }
        //解绑技师与用户的号码
        MiddleNumberUnbindVo middleNumberUnbindVo = new MiddleNumberUnbindVo();
        middleNumberUnbindVo.setBindNumberA(middleRelation.getBindNumberA());
        middleNumberUnbindVo.setBindNumberB(middleRelation.getBindNumberB());
        voiceUtil.middleNumberUnbind(middleNumberUnbindVo);
       //删除记录
        return toAjax(middleRelationService.deleteMiddleRelationById(id));
    }
}
